﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Trans.member
{
    public partial class Manage_access : Form
    {
        private int mem_id;
        private const int ACCESS_ADD = 1, ACCESS_MANAGE = 2, ACCESS_QUERY = 4;
        public Manage_access()
        {
            InitializeComponent();
        }

        private void Manage_access_Load(object sender, EventArgs e)
        {
            vw_memberTableAdapter.Fill(dataSet_view.vw_member);
            tb_opertorTableAdapter.Fill(dataSet.tb_opertor);
            
            TreeNode n;
            TreeNode m;
            foreach (DataRow row in dataSet_view.vw_member)
            {
                //添加部门
                if(tv_member.Nodes.Find((string)row["depart_name"],false).Length==0)
                {
                   n=new TreeNode();
                    n.Name=(string)row["depart_name"];
                    n.Text=(string)row["depart_name"];
                    tv_member.Nodes.Add(n);
                }
                n = tv_member.Nodes.Find((string)row["depart_name"], false)[0];
                //添加职位
                if(n.Nodes.Find((string)row["role"],false).Length==0)
                {
                    m = new TreeNode();
                    m.Text = (string)row["role"];
                    m.Name = (string)row["role"];
                    n.Nodes.Add(m);
                }
                n = n.Nodes.Find((string)row["role"], false)[0];
                m=new TreeNode();
                m.Text=(string)row["name"];
                m.Name=(string)row["name"];
                m.Tag=row["id"];
                n.Nodes.Add(m);
            }
            mem_id = 0;
        }

        private void tv_member_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
            if(e.Node.Nodes.Count==0)
            {
                mem_id = (int)e.Node.Tag;
                
                tv_access_init();
            }
        }
       
        private void tv_access_init()
        {
            int id = mem_id;

            DataRow row;
            row = (DataRow)dataSet.tb_opertor.FindByid(id);
            foreach (TreeNode n in tv_access.Nodes)
            {

                byte sum;
                //access_add,manage,query
                bool ac_add, ac_man, ac_que;
                sum = (byte)row[n.Index + 1];
                ac_add = (sum & ACCESS_ADD) > 0;
                ac_man = (sum & ACCESS_MANAGE) > 0;
                ac_que = (sum & ACCESS_QUERY) > 0;
                n.Nodes[0].ImageIndex = ac_add ? 0 : 1;

                n.Nodes[1].ImageIndex = ac_que ? 0 : 1;

                n.Nodes[2].ImageIndex = ac_man ? 0 : 1;

            }
            {

                TreeNode n;
                n = tv_access.Nodes[tv_access.Nodes.Count - 1];
                byte sum = (byte)row[n.Index + 1];
                bool ac_restore = (sum & session.Access_Const.SYS_ACCESS_RESTORE) > 0,
                     ac_backup = (sum & session.Access_Const.SYS_ACCESS_BACKUP) > 0,
                     ac_init = (sum & session.Access_Const.SYS_ACCESS_INIT) > 0,
                     ac_setaccess = (sum & session.Access_Const.SYS_ACCESS_SETACCESS) > 0;

                n.Nodes[0].ImageIndex = ac_restore ? 0 : 1;
                n.Nodes[0].SelectedImageIndex = n.Nodes[0].ImageIndex;
                n.Nodes[1].ImageIndex = ac_backup ? 0 : 1;
                n.Nodes[1].SelectedImageIndex=n.Nodes[1].ImageIndex;
                n.Nodes[2].ImageIndex = ac_init ? 0 : 1;
                n.Nodes[2].SelectedImageIndex=n.Nodes[2].ImageIndex;
                n.Nodes[3].ImageIndex = ac_setaccess ? 0 : 1;
                n.Nodes[3].SelectedImageIndex = n.Nodes[3].ImageIndex;
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {

            try
            {
                tb_opertorTableAdapter.Update(dataSet.tb_opertor);
                MessageBox.Show("操作成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("出错啦" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

        private void tv_access_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            DataRow row;

            row = (DataRow)dataSet.tb_opertor.FindByid(mem_id);
            if (e.Node.Nodes.Count == 0)
            {
                TreeNode n = e.Node;
                byte sum;
                byte ac = 0;
                sum = (byte)row[n.Parent.Index + 1];
                n.ImageIndex = 1 - n.ImageIndex;
                n.SelectedImageIndex = n.ImageIndex;
                #region 权限
                if (n.Parent.Text == "系统管理")
                {
                    switch (n.Index)
                    {
                        case 0:
                            {
                                ac = session.Access_Const.SYS_ACCESS_RESTORE;
                                break;
                            }
                        case 1:
                            {
                                ac = session.Access_Const.SYS_ACCESS_BACKUP;
                                break;
                            }
                        case 2:
                            {
                                ac = session.Access_Const.SYS_ACCESS_INIT;
                                break;
                            }
                        case 3:
                            {
                                ac = session.Access_Const.SYS_ACCESS_SETACCESS;
                                break;
                            }
                    }
                }
                else
                {
                    switch (n.Index)
                    {
                        case 0:
                            {
                                ac = ACCESS_ADD;
                                break;
                            }
                        case 1:
                            {
                                ac = ACCESS_QUERY;
                                break;
                            }
                        case 2:
                            {
                                ac = ACCESS_MANAGE;
                                break;
                            }
                    }
                }
                #endregion
                if (n.ImageIndex == 0)
                {
                    /*允许状态*/

                    row[n.Parent.Index + 1] = sum | ac;
                }
                else
                {
                    /*不允许状态*/
                    row[n.Parent.Index + 1] = sum & (~ac);
                }
            }
        }

        
    }
}
